<template>
  <AssetHolderSelectList
      ref="refHoldList"
      :tag="'客户'"
      :name='props.entry.name'
      :columns="columns"
      :load-holders="loadAssetHolders"
      :join-holders="joinAssetHolders"
      :load-join-holders="loadJoinAssetHolders"
      :comment="props.comment"
  />
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {useLoadAssign} from '@/utils/UseLoadAssign'
import AssetHolderSelectList from '@/widget/holder/AssetHolderSelectList.vue'
import {deleteAsset, removeAssetOfHolder} from "@/apis";
import {msgInfo, msgWarn, showInfo} from "@/utils/InfoUtil";
import {TYPES} from "@/stores/conf";
import {getStateOptions, getStateTag, getStateText} from "@/utils/SystemUtil";

const refHoldList = ref();

const props = withDefaults(
    defineProps<{
      entry: any,
      comment?: string
    }>(),
    {}
)

const {loadHolders, joinHolders, loadJoinHolders, removeAssetFromHolder} = useLoadAssign(props.entry);

const getTargetId = () => {
  return '';
}

const columns = ref([
  {
    id: 'id',
    name: '标识',
    hideCol: true,
    show: false
  },
  {
    id: 'name',
    name: '客户名称',
    show: true
  },
  {
    id: 'state',
    name: '状态',
    show: true,
    tag: 'true',
    type: 'select',
    tagType: (tag: number) => { return getStateTag(tag); },
    tagText: (tag: number) => { return getStateText(tag); },
    options: getStateOptions()
  },
  {
    id: 'createTime',
    name: '创建时间',
    show: true
  },
  {
    id: 'operation',
    name: '操作',
    show: true,
    canEdit: false,
    hideCol: true,
    fixed: 'right',
    operations: [
      {
        type: 'confirm',
        name: '移除',
        tips: () => { return '确认移除？' },
        confirm: async (data: any, row: any, index: number) => {
          await removeAssetFromHolder(getTargetId(), TYPES.CLIENT, {
            ...props.entry
          }, row);
          refresh();
        },
        cancel: (data: any, index: number) => {
          const row = data[index];
        }
      }
    ]
  }
])

const loadAssetHolders = async (params: any) => {
  return await loadHolders(getTargetId(), TYPES.CLIENT, params || {})
}

const joinAssetHolders = async (items: any) => {
  return await joinHolders(getTargetId(), TYPES.CLIENT, items)
}

const loadJoinAssetHolders = async (params: any) => {
  return await loadJoinHolders(getTargetId(), TYPES.CLIENT, params || {})
}

const refresh = () => {
  refHoldList.value?.refresh();
}

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}
</style>
